<?php

namespace app\adminapi\service;

use app\adminapi\model\sysdict\SysDictDataDO;
use app\adminapi\model\sysdict\SysDictTypeDO;
use app\common\exception\OtherException;

class SysDictDataService
{
    public function list(array $param)
    {
        $where = [];
        if (isset($param['dictDataName'])) {
            $where[] = ['dict_data_name', 'like', '%' . $param['dictDataName'] . '%'];
        }
        if (isset($param['dictDataValue'])) {
            $where[] = ['dict_data_value', 'like', '%' . $param['dictDataValue'] . '%'];
        }
        if (isset($param['dictCode'])) {
            $where[] = ['dict_type', 'like', '%' . $param['dictCode'] . '%'];
        }
        $paginate['list_rows'] = $param['pageSize'];
        if (isset($param['pageNo'])) {
            $paginate['page'] = $param['pageNo'];
        }
        $result = (new SysDictDataDO())
            ->where($where)
            ->paginate($paginate)
            ->toArray();
        return [
            'total' => $result['total'],
            'list' => $result['data']
        ];
    }

    public function addSysDictData(array $sysDictDataDO)
    {
        $this->validDictTypeExist($sysDictDataDO['dictType']);
        $this->validDictDataValueExist($sysDictDataDO['dictType'], $sysDictDataDO['dictDataValue'], null);
        return SysDictDataDO::create($sysDictDataDO);
    }

    public function deleteSysDictData(int $dictDataId)
    {
        $this->validDictDataExist($dictDataId);
        return SysDictDataDO::destroy($dictDataId);
    }

    public function updateSysDictData(array $sysDictDataDO)
    {
        $this->validDictDataExist($sysDictDataDO['dictDataId']);
        $this->validDictDataValueExist($sysDictDataDO['dictType'], $sysDictDataDO['dictDataValue'], $sysDictDataDO['dictDataId']);
        return SysDictDataDO::update($sysDictDataDO);
    }

    public function getSysDictData(int $dictDataId)
    {
        $this->validDictDataExist($dictDataId);
        return SysDictDataDO::find($dictDataId);
    }

    public function listByType(string $dictType)
    {
        // 根据dictType获取字典编码
        $this->validDictTypeExist($dictType);
        $list = (new SysDictDataDO())->where(['dict_type' =>$dictType])->select()->toArray();
        return $list;
    }

    private function validDictTypeExist(string $dictType)
    {
        $exist = (new SysDictTypeDO())->where(['dict_code' => $dictType])->find();
        if (!$exist) {
            throw new OtherException('字典类型不存在');
        }
    }


    private function validDictDataValueExist(string $dictType, string $dictDataValue, $dictDataId)
    {
        $where = [
            ['dict_type', '=', $dictType],
            ['dict_data_value', '=', $dictDataValue]
        ];
        if ($dictDataId) {
            $where[] = ['dict_data_id', '<>', $dictDataId];
        }
        $sysDictDataDO = (new SysDictDataDO())->where($where)->find();
        if ($sysDictDataDO) {
            throw new OtherException('字典数据值已存在');
        }
    }

    private function validDictDataExist(int $dictDataId)
    {
        $sysDictDataDO = SysDictDataDO::find($dictDataId);
        if (!$sysDictDataDO) {
            throw new OtherException('字典数据不存在');
        }
    }
}